<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<div class="setp_top">
	<div class="setp_top_btn">
		&nbsp;<s:if test="#request.isCreate == 1">
			<input type="button" class="btn_ok" onclick="nextStep()" />
		</s:if>
		<input type="button" class="btn_cancel" onclick="prevStep()" />
	</div>
	<s:property value="#typeTxt"/>&nbsp;<s:property value="#workStatusTxt"/>
</div>
<div id="div_operator" class="setp_main">
	<div style="margin-top: -10px; float: left; position:relative; width: 95%; font-size: 12px; color: black;">
		<div style="position:absolute; right:0; bottom: 0;">
			<div id="div_file_add" style="float: right; margin-left: 8px;cursor: pointer;" onclick="openWorkDoc()">
				<img src="${pageContext.request.contextPath}/cts/images/flie_add.png"/><br/>
				新增
			</div>
			<div id="div_file_delete" style="float: right; margin-left: 8px;cursor: pointer;" onclick="deleteWorkDoc()">
				<img src="${pageContext.request.contextPath}/cts/images/flie_del.png"/><br/>
				删除
			</div>
			<div id="div_file_export" style="float: right; margin-left: 8px;cursor: pointer;" onclick="exportWorkDoc()">
				<img src="${pageContext.request.contextPath}/cts/images/flie_export.png"/><br/>
				导出
			</div>
			<div id="div_file_edit" style="float: right; margin-left: 8px;cursor: pointer;" onclick="editWorkDoc()">
				<img src="${pageContext.request.contextPath}/cts/images/flie_edit.png"/><br/>
				修改
			</div>
		</div>
		<s:iterator value="%{#request.workDocs1}" status="sta">
			<s:if test="option4 != null">
				<div class="flie_div" onclick="activeFlie(this)" doctype="0" option4="<s:property value="option4"/>" workDocId="<s:property value="option3"/>">
					<div class="flie_<s:property value="option1"/>">
						<img src="${pageContext.request.contextPath}/cts/images/unread_<s:property value="option2"/>.png" style="margin-left: 30px; margin-top: 3px;<s:if test="option2 == null">display: none;</s:if>" />
					</div>
					<div style="width: 72px" class="nowrap" title="<s:property value="name"/>">
						<s:property value="name"/>
					</div>
				</div>
			</s:if>
		</s:iterator>
		<s:iterator value="%{#request.workDocs2}" status="sta">
			<s:if test="option4 != null">
				<div class="flie_div" onclick="activeFlie(this)" doctype="1" option4="<s:property value="option4"/>" workDocId="<s:property value="id"/>">
					<div class="flie_<s:property value="option1"/>">
					</div>
					<div style="width: 72px" class="nowrap" title="<s:property value="option3"/>">
						<s:property value="option3"/>
					</div>
				</div>
			</s:if>
		</s:iterator>
	</div>
	<!-- 编辑器 -->
	<div id="div_file_editor" style="width:95%; min-height:642px;_height:642px;border:1px solid #676767; float: left; margin-top: 3px;background-color: #D3DBAC;"></div>
</div>
<div id="file_add_div" class="popdiv_inner" style="width: 500px;">
	<div class="popdiv_inner_outter"></div>
	<div class="popdiv_close"></div>
	<div class="popdiv_handle" title="鼠标左键按住可拖动"></div>
	<div class="popdiv_content"></div>
</div>
<script type="text/javascript">
	var workId = '<s:property value="work.id"/>';
	
	$(function(){
		activeFlie($('div.flie_div:first')[0]);
	})
	
	function activeFlie(obj){
		if(obj){
			$('div.flie_active_div').attr('class','flie_div');
			obj.className = 'flie_active_div';
			
			$('#div_file_edit').show();
			$('#div_file_add').show();
			$('#div_file_export').show();
			$('#div_file_delete').show();
			var selectedObj = $(obj);
			var doctype = selectedObj.attr('doctype');
			var option4 = selectedObj.attr('option4');
			var workDocId = selectedObj.attr('workDocId');
			if(doctype == 1){
				$('#div_file_export').hide();
				if(option4 != 1){
					$('#div_file_edit').hide();
					$('#div_file_delete').hide();
				}
				var fileName = $.trim(selectedObj.find('div:last').text());
				var string = '<div style="padding: 6px;font-size: 14px;margin-top: 20px" align="center">自定义上传文件仅支持下载，点击文件名<a style="color: blue" href="downloadWorkDoc?workDocId='+workDocId+'">'+fileName+'</a>下载<div>';
				$('#div_file_editor').html(string);
			}else{
				if(option4 != 1){
					$('#div_file_delete').hide();
				}
				$('#div_file_editor').html('');
				$.post("cts/loadWorkDocContent.action?workDocId=" + workDocId + '&option4=' + option4, null, function(data){
					if(data != ''){
						$('#div_file_editor').html(data);
					}
			    });
			}
		}else{
			$('#div_operator').css('margin-top', '80px');
			$('#div_file_editor').css('background-color', '#fff').html('');
			$('#div_file_edit').hide();
			$('#div_file_export').hide();
			$('#div_file_delete').hide();
		}
	}
	
	function openWorkDoc(){
		$('#file_add_div div.popdiv_handle').text('新增文件');
		openPopDiv('file_add_div', 'selectDocs', '100');
	}
	
	function editWorkDoc(){
		var selectedObj = $('div.flie_active_div');
		var doctype = selectedObj.attr('doctype');
		if(doctype == 0){
			$('#div_file_editor').html('');
			var option4 = selectedObj.attr('option4');
			var workDocId = selectedObj.attr('workDocId');
			$.post("cts/loadWorkDocContent.action?isEdit=1&workDocId=" + workDocId + '&option4=' + option4, null, function(data){
				if(data != ''){
					$('#div_file_edit').hide();
					$('#div_file_export').hide();
					$('#div_file_editor').html(data);
				}
		    });
		}else if(doctype == 1){
			var workDocId = selectedObj.attr('workDocId');
			$('#file_add_div div.popdiv_handle').text('修改共享访客');
			openPopDiv('file_add_div', 'selectDocs?workDocId=' + workDocId, '150');
		} 
	}
	
	function deleteWorkDoc(){
		var workDocId = $('div.flie_active_div').attr('workDocId');
		if(confirm('确定要删除该文件吗？')){
			$.post("cts/deleteWorkDoc.action?workDocId=" + workDocId, null, function(data){
				if(data == 1){
					$('div.flie_active_div').remove();
					activeFlie($('div.flie_div:first')[0]);
				}else{
					alert('操作失败，请稍候再试！');
				}
		    });
		}
	}
	
	function exportWorkDoc(){
		var obj = window.frames['iframe_workdoccontent'];
		if(obj && obj.exportPdf){
			obj.exportPdf();
		}
	}
	
	function changeFileAddType(obj){
		if(obj.value == 0){
			$('#div_file_addtype_1').show();
			$('#div_file_addtype_2').hide();
		}else{
			$('#div_file_addtype_1').hide();
			$('#div_file_addtype_2').show();
		}
	}
	
	function saveWorkDoc(){
		var workDocId = $('#workDocId').val();
		if(!workDocId){
			var fileAddType = $(':radio[name=fileAddType]:checked').val();
			if(fileAddType == 1){
				var image = $('#myFile').val();
		   		if ($.trim(image) == '' || image.indexOf('.') == -1){
		   			alert('上传的文件不能为空');
		   			return;
		   		}
				var extention = image.substring(image.lastIndexOf('.') + 1);
				var suffixarr = ",jpg,jpeg,bmp,gif,png,pdf,doc,docx,ppt,pptx,xls,xlsx,rar,zip,7z,";
				if(suffixarr.indexOf(',' + extention.toLowerCase() + ',') == -1){
					alert('仅支持上传后缀名为：' + suffixarr.substring(1, suffixarr.length - 1) +  '的文件');
					return;
				}
			}else{
				var docTemplateId = $(':radio[name=rdo_doctemplates]:checked').val();
				if(!docTemplateId || docTemplateId == ''){
					alert('请选择一个文档模板');
					return;
				}
				if($.trim($('#workDocName').val()) == ''){
					alert('文档名称不能为空');
					$('#workDocName').focus();
					return;
				}
			}
		}
		var right = document.getElementById("employeeRight"); 
	    for(i=0; i < right.length; i++){
		    right[i].selected = true;
	    }
		var form = document.getElementById("fileAddForm");
        form.action = "workStep7.action?save=true&workId=" + workId;
        form.submit();
	}
	
	function saveWorkDocContent(){
		var right = document.getElementById("employeeRight"); 
		if(right){
		    for(i=0; i < right.length; i++){
			    right[i].selected = true;
		    }
		}
		var form = document.getElementById("workDocContentForm");
        form.action = "workStep7.action?save=true&workId=" + workId;
        form.submit();
	}
	
	function setWorkDocName(obj){
		$('#workDocName').val($.trim($(obj).parent().text()));
	}
</script>
